# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper

  def flash_eni
    o = String.new
    if flash[:error]
      o << "<div id=\"flash_error\">#{flash[:error]}</div>\n"
    end
    if flash[:notice]
      o << "<div id=\"flash_notice\">#{flash[:notice]}</div>\n"
    end
    if flash[:info]
      o << "<div id=\"flash_info\">#{flash[:info]}</div>\n"
    end
    return o
  end
  
  def login_link_or_username
    if session[:user].nil?
      link_to "Anmelden", :controller => "user", :action => "login"
    else
      o = "Angemeldet als: "
      o << (link_to "#{session[:user].vorname + " " + session[:user].nachname}", :controller => "user", :action => "show")
      o << " (Admin)" if session[:user][:admin] > 0
      o << " / "
      o << (link_to "Abmelden", :controller => "user", :action => "logout")
    end
  end
  
  def meine_termine_link
    o = String.new
    o << (link_to "Meine Termine", :controller => "cal", :action => "mydates") + " | " if session[:user]
  end
end

module ActionView::Helpers::ActiveRecordHelper
  
  def error_messages_for(*params)
    options = params.last.is_a?(Hash) ? params.pop.symbolize_keys : {}
    objects = params.collect { |object_name| instance_variable_get("@#{object_name}") }.compact
    count   = objects.inject(0) { |sum, object| sum + object.errors.count }
    unless count == 0
      o = "<div id=\"flash_error\"><b>Bitte übrprüfen Sie die folgenden Angaben:</b><br><ul>"
      objects.each do |object|
        object.errors.full_messages.each do |msg|
          o << "<li>#{msg}</li>"
        end
      end
      o << "</ul></div>\n"
    end
  end
end
